#!/bin/bash

function config_osc()
{
    # config osc
    cat << END1 > /root/.oscrc
[general]
apiurl = https://build.openeuler.org
no_verify = 1

[https://build.openeuler.org]
user = Admin
pass = openeuler123!@#

END1

    expect -c "
        set timeout 300
        spawn osc api /about
            expect {
                \"Enter choice*:\" {send \"2\r\"; exp_continue}
            }
    "
}

function check_env()
{
    set +ue
    # if -n, determine whether the string(OBS_SERVER_HOST) is empty
    if [ -n "${OBS_SERVER_HOST}" ]; then
        if ! cat /etc/hosts | grep ${OBS_SERVER_HOST}; then
            sed -i '/openeuler-obs/d' /etc/hosts
            echo "${OBS_SERVER_HOST} openeuler-obs" >> /etc/hosts
            rm -rf /root/.config/osc/*
            config_osc
        fi
    else
        if ! cat /etc/hosts | grep 'openeuler-obs'; then
            echo "100.109.85.108 openeuler-obs" >> /etc/hosts
            rm -rf /root/.config/osc/*
            config_osc
        fi
    fi

    set -ue

    if ! cat /root/.oscrc | grep "https://build.openeuler.org"; then
        config_osc
    fi
    if ! osc api /about; then
        config_osc
    fi
}

function check_obs_meta()
{
    set +e
}

function update_obs_meta()
{
    git_config
    commit_msg=$1
    branch=$2
    pushd ${git_root}/obs_meta
    git_commit "$commit_msg"
    git_push $branch
    popd
}
